<?php

namespace forfun\controller;

use forfun\controller\vo\CoupletReplyForm;
use forfun\controller\vo\CoupletReplyModifyForm;
use forfun\service\CoupletService;
use restphp\http\RestHttpRequest;

/**
 * @RequestMapping ("/api/v1/couplets")
 */
class ApiCoupletController extends ApiBaseController {
    /**
     * @Get("/today")
     * @Logon
     * @return void
     */
    public function today() {
        $this->_success(CoupletService::getCoupletToday());
    }

    /**
     * @Get ("/days/{day})
     * @return void
     */
    public function getByDate()
    {
        $day = RestHttpRequest::getPathValue('day');
        $this->_success(CoupletService::getCoupletByDate($day));
    }

    /**
     * @Post ("/actions/reply")
     * @Logon
     * @return void
     */
    public function reply() {
        $coupletReply = RestHttpRequest::getBody(CoupletReplyForm::class, true);
        CoupletService::replyCouplet($coupletReply);
        $this->_success();
    }

    /**
     * @Get ("/{id}/replies")
     * @Logon
     * @return void
     */
    public function getReplyList() {
        $coupletId = RestHttpRequest::getPathValue('id');
        $page = RestHttpRequest::getRequest("page", 1);
        $this->_success(CoupletService::getReplyList($coupletId, $page));
    }

    /**
     * @Delete("/{coupletId}/replies/{id}")
     * @Logon
     * @return void
     */
    public function deleteReply() {
        $coupletId = RestHttpRequest::getPathValue('coupletId');
        $id = RestHttpRequest::getPathValue('id');
        CoupletService::deleteReply($coupletId, $id);
    }

    /**
     * @Put ("/{coupletId}/replies/{id}")
     * @Logon
     * @return void
     */
    public function modifyReply() {
        $coupletId = RestHttpRequest::getPathValue('coupletId');
        $id = RestHttpRequest::getPathValue('id');
        $coupletReply = RestHttpRequest::getBody(CoupletReplyModifyForm::class, true);
        CoupletService::modifyReply($coupletId, $id, $coupletReply);
    }
}